package de.hdu.myjobapp.ctrl;

import java.util.HashMap;

import org.apache.log4j.Logger;

import com.vaadin.Application.UserChangeEvent;
import com.vaadin.Application.UserChangeListener;
import com.vaadin.ui.*;

import de.hdu.myjobapp.model.Application;
import de.hdu.myjobapp.model.User;
import de.hdu.myjobapp.view.MyJobApplication;
import de.hdu.myjobapp.view.component.Berichte;
import de.hdu.myjobapp.view.component.Bewerbung;
import de.hdu.myjobapp.view.component.Firma;
import de.hdu.myjobapp.view.component.FirmaEdit;
import de.hdu.myjobapp.view.component.Header;
import de.hdu.myjobapp.view.component.Menu;
import de.hdu.myjobapp.view.component.UserView;
import de.hdu.myjobapp.view.component.WelcomeView;

import de.hdu.myjobapp.view.view.AbstractView;
import de.hdu.myjobapp.view.view.ApplicationEditor;
//import de.hdu.myjobapp.view.view.ChannelView;
import de.hdu.myjobapp.view.view.Uebersicht;


public class UiHandler extends VerticalLayout implements UserChangeListener {

	/**
	 * get a logger
	 */
	private transient Logger log = Logger.getLogger(UiHandler.class);
	
	private static final long serialVersionUID = 1L;
	// Portions of the application that exist at all times.
	private Header header;
	private WelcomeView defaultView;
	// The views that are shown to logged users.
	private UserView userView;
	//private ChannelView javaView;
	//private ChannelView ubuntuView;
	private Firma firmview;
	private FirmaEdit firmNeu;
	private Bewerbung bewerbview;
	private Uebersicht uebersichtView;
	private ApplicationEditor applicationEditor;
	private Berichte bericht;

	// Components shown to logged users.
	private Menu menu;
	private VerticalSplitPanel menusplit;

	// Used to keep track of the current main view.
	private HashMap<String, AbstractView> viewList = new HashMap<String, AbstractView>();

	/**
	 * Constructor of the handler. Creates and styles all default components.
	 * 
	 * @param window main window of the application
	 */
	public UiHandler(Window window) {
		header = new Header();
		defaultView = new WelcomeView();

		setSizeFull();
		setMargin(false);

		// Add the main components for the UI.

		addComponent(header);
		addComponent(defaultView);
		

		setComponentAlignment(header, Alignment.TOP_CENTER);
		setComponentAlignment(defaultView, Alignment.TOP_CENTER);

		setExpandRatio(defaultView, 1);
		this.setSpacing(true);
		window.setLayout(this);
		
	}

	/**
	 * All the UI logic in the event that a user logs in.
	 * Loads the default User view
	 */
	public void userLoggedIn() {

		// initialization of the user specific portions of the UI and
		// add a split panel to differentiate between menu and main view.
		//MyJobApplication.getProject().getMainWindow().showNotification(((User)MyJobApplication.getProject().getUser()).getUserName());
		
		log.debug("UserLogged In");
		userView = new UserView();
		menu = new Menu();
		//javaView = new ChannelView("#java"); //demodata
		//ubuntuView = new ChannelView("#ubuntu");//demodata
		firmview = new Firma();
		firmNeu = new FirmaEdit();
		bewerbview = new Bewerbung();
		uebersichtView = new Uebersicht();
		applicationEditor = new ApplicationEditor();
		bericht = new Berichte();

		// Hard code adding of the user specific views to the view list.
		viewList.clear();
		//viewList.put("#java", javaView);
		//viewList.put("#ubuntu", ubuntuView);
		viewList.put("User View", userView);
		viewList.put("firmaview", firmview);
		viewList.put("FirmaNeu", firmNeu);
		viewList.put("bewerbungview",bewerbview);
		viewList.put("uebersicht", uebersichtView);
		viewList.put("applicationEditor",applicationEditor);
		viewList.put("berichte", bericht);


		menusplit = new VerticalSplitPanel();

		// Adds the splitbar to the layout.
		addComponent(menusplit, 1);

		// Visual modifiers of the splitpanel.
		setExpandRatio(menusplit, 1);
		menusplit.setSplitPosition(100, VerticalSplitPanel.UNITS_PIXELS);

		// Remove the default view and show the user specifc one.
		removeComponent(defaultView);
		addComponent(userView);

		// Set the menu on the left side of the split.
		menusplit.setFirstComponent(menu);

		// Set the Welcome View on the right side.
		setMainView(userView);

		// Inform the different parts of the UI that the user has logged in.
		header.userLoggedIn();
		

	}

	/**
	 * Reverts back to the default view and removes all the user specific
	 * content.
	 */
	public void userLoggedOut() {
		log.debug("User Logged Out");
		// If components exist.
		try {
			// Remove the split panel including menu and userView.
			removeComponent(menusplit);

			// Add the default view.
			addComponent(defaultView, 1);
			setExpandRatio(defaultView, 1);

			// Tell the other UI components that the user logged off.
			header.userLoggedOut();

		} catch (Exception e) {

		}
	}

	/**
	 * Updates the main view when the user clicks an item in the menu.
	 * 
	 * @param The View saved in the hashmap
	 */
	public void switchView(String viewName) {
		if (viewName == "bewerbungview"){
			bewerbview.reload();
		}
		if (viewName == "firmaview"){
			firmview.reload();
		}
		if (viewName == "FirmaNeu"){
			firmNeu.clean();
		}
		if (viewName == "uebersicht"){
			uebersichtView = new Uebersicht();
			viewList.remove("uebersichtView");
			viewList.put("uebersicht", uebersichtView);
		}
		if (viewName == "applicationEditor"){
			applicationEditor = new ApplicationEditor();
			viewList.remove("applicatonEditor");
			viewList.put("applicationEditor",applicationEditor);
		}
		setMainView(viewList.get(viewName));
	}

	/**
	 * Helper method for setting the main view.
	 * 
	 * @param c View that we want to display
	 */
	public void setMainView(AbstractView c) {

		menusplit.setSecondComponent(c);

	}

//	/*
//	 * Closes the view and removes it from the menu.
//	 */
//	public void closeCurrentView() throws Exception {
//
//		// Remove the view from the view list.
//		viewList.remove(menu.getTree().getValue().toString());
//		setMainView(userView);
//		menu.removeCurrentTreeSelection();
//
//	}

	public void applicationUserChanged(UserChangeEvent event) {


				if (event.getNewUser() == null) {
					userLoggedOut();
				} else {
					userLoggedIn();
				}

	}
	/**
	 * Helpermethod to load the applicationEditor Form either with new Data or existing editing Data.
	 * @param application The Application which should be edited
	 * @param neworupdate If true then it is a new application, if False then the application is going to be edited
	 */
	public void setApplicationEditorApp(Application application,Boolean neworupdate){
		/*
		 * Wenn keine Application übergeben wird, bzw. ein falsches Objekt oder ein Fehler übergeben wird,
		 *  dann wird eine Neue Applikation instanziert und leer übergeben
		 * 
		 */
		try{
			applicationEditor.setApplication(application,neworupdate);
		}catch(Exception e){
			applicationEditor.setApplication(null,true);
		}
		
	}
	

}
